package com.xgy.userauthsystem.util;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 邮件发送工具类
 */
@Component
public class EmailApi {
    // 日志记录器
    private static final Logger logger = LoggerFactory.getLogger(EmailApi.class);

    @Resource
    private JavaMailSender mailSender; // 注入JavaMailSender bean

    @Value("${spring.mail.username}")
    private String from; // 发件人

    /**
     * 发送一般邮件
     * @param subject 主题
     * @param content 内容
     * @param to 收件人
     * @return 是否成功
     */
    public boolean sendGeneralEmail(String subject, String content, String... to){
        try {
            // 创建邮件消息
            SimpleMailMessage message = new SimpleMailMessage();
            // 设置发件人
            message.setFrom(from);
            // 设置收件人
            message.setTo(to);
            // 设置邮件主题
            message.setSubject(subject);
            // 设置邮件内容
            message.setText(content);

            // 发送邮件
            mailSender.send(message);

            return true;
        } catch (Exception e) {
            logger.error("Failed to send email", e);
            return false;
        }
    }

}